@echo off
:: Matrix Service Discovery Windows 启动脚本

setlocal enabledelayedexpansion

:: 设置颜色
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

:: 日志函数
:log_info
echo %BLUE%[INFO]%NC% %~1
goto :eof

:log_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof

:log_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:log_error
echo %RED%[ERROR]%NC% %~1
goto :eof

:: 检查依赖
:check_dependencies
call :log_info "检查依赖工具..."

where dotnet >nul 2>nul
if %errorlevel% neq 0 (
    call :log_error "dotnet CLI 未安装"
    exit /b 1
)

where docker >nul 2>nul
if %errorlevel% neq 0 (
    call :log_warning "Docker 未安装，将跳过容器化启动"
    set USE_DOCKER=false
) else (
    set USE_DOCKER=true
)

call :log_success "依赖检查完成"
goto :eof

:: 设置环境变量
:setup_environment
call :log_info "设置环境变量..."

if not defined ASPNETCORE_ENVIRONMENT (
    set ASPNETCORE_ENVIRONMENT=Development
)

set DOTNET_ENVIRONMENT=%ASPNETCORE_ENVIRONMENT%

call :log_success "环境设置完成: %ASPNETCORE_ENVIRONMENT%"
goto :eof

:: 检查服务状态
:check_services
call :log_info "检查服务状态..."

:: 检查端口占用
netstat -an | findstr ":5433" >nul 2>nul
if %errorlevel% equ 0 (
    call :log_warning "PostgreSQL 端口 5433 已被占用"
) else (
    call :log_info "PostgreSQL 端口 5433 可用"
)

netstat -an | findstr ":6380" >nul 2>nul
if %errorlevel% equ 0 (
    call :log_warning "Redis 端口 6380 已被占用"
) else (
    call :log_info "Redis 端口 6380 可用"
)

netstat -an | findstr ":5673" >nul 2>nul
if %errorlevel% equ 0 (
    call :log_warning "RabbitMQ 端口 5673 已被占用"
) else (
    call :log_info "RabbitMQ 端口 5673 可用"
)

netstat -an | findstr ":5001" >nul 2>nul
if %errorlevel% equ 0 (
    call :log_warning "ServiceDiscovery 端口 5001 已被占用"
) else (
    call :log_info "ServiceDiscovery 端口 5001 可用"
)
goto :eof

:: 构建应用
:build_application
call :log_info "构建应用..."

cd /d "%~dp0.."

dotnet build --configuration Release --no-restore
if %errorlevel% neq 0 (
    call :log_error "应用构建失败"
    exit /b 1
)

call :log_success "应用构建成功"
goto :eof

:: 数据库迁移
:run_migrations
call :log_info "运行数据库迁移..."

cd /d "%~dp0.."

dotnet run --project ..\MatrixServiceDiscovery.DbMigrator --configuration Release --no-build
if %errorlevel% neq 0 (
    call :log_error "数据库迁移失败"
    exit /b 1
)

call :log_success "数据库迁移成功"
goto :eof

:: 启动基础设施服务
:start_infrastructure
if "%USE_DOCKER%"=="true" (
    call :log_info "启动基础设施服务 (Docker)..."

    cd /d "%~dp0.."

    docker-compose up -d postgres redis rabbitmq
    if %errorlevel% neq 0 (
        call :log_error "基础设施服务启动失败"
        exit /b 1
    )

    call :log_success "基础设施服务启动成功"

    :: 等待服务就绪
    call :log_info "等待服务就绪..."
    timeout /t 10 /nobreak >nul
) else (
    call :log_warning "跳过Docker基础设施启动，请确保 PostgreSQL、Redis、RabbitMQ 已手动启动"
)
goto :eof

:: 启动应用
:start_application
call :log_info "启动 Matrix Service Discovery 应用..."

cd /d "%~dp0.."

:: 创建必要的目录
if not exist "logs" mkdir logs
if not exist "data" mkdir data

if "%USE_DOCKER%"=="true" (
    :: Docker 模式启动
    call :log_info "使用 Docker 启动应用..."
    docker-compose up -d matrix-service-discovery

    :: 等待应用启动
    call :log_info "等待应用启动..."
    timeout /t 15 /nobreak >nul

    :: 检查应用健康状态
    set attempts=0
    set max_attempts=30

    :check_health
    set /a attempts+=1
    curl -f http://localhost:5001/health >nul 2>nul
    if %errorlevel% equ 0 (
        call :log_success "应用启动成功！"
        goto :health_success
    )

    if %attempts% lss %max_attempts% (
        call :log_info "等待应用启动... (!attempts!/!max_attempts!)"
        timeout /t 2 /nobreak >nul
        goto :check_health
    )

    call :log_error "应用启动超时"
    exit /b 1

    :health_success
) else (
    :: 本地模式启动
    call :log_info "使用本地模式启动应用..."
    dotnet run --configuration Release
)
goto :eof

:: 显示访问信息
:show_access_info
call :log_success "==================================="
call :log_success "Matrix Service Discovery 已启动"
call :log_success "==================================="
call :log_info "应用访问地址:"
call :log_info "  - API 地址: http://localhost:5001"
call :log_info "  - API 文档: http://localhost:5001/swagger"
call :log_info "  - 健康检查: http://localhost:5001/health"
call :log_info ""
call :log_info "基础设施服务:"
call :log_info "  - PostgreSQL: localhost:5433"
call :log_info "  - Redis: localhost:6380"
call :log_info "  - RabbitMQ: localhost:5673"
call :log_info "  - RabbitMQ 管理界面: http://localhost:15673"
call :log_info ""
call :log_info "监控服务 (如果启用):"
call :log_info "  - Prometheus: http://localhost:9091"
call :log_info "  - Grafana: http://localhost:3001 (admin/admin123)"
call :log_success "==================================="
goto :eof

:: 清理函数
:cleanup
call :log_info "执行清理操作..."

if "%USE_DOCKER%"=="true" (
    cd /d "%~dp0.."
    docker-compose down
)

call :log_success "清理完成"
goto :eof

:: 主函数
:main
set command=%1
if "%command%"=="" set command=start

if "%command%"=="start" (
    call :log_info "启动 Matrix Service Discovery..."
    call :check_dependencies
    call :setup_environment
    call :check_services
    call :start_infrastructure
    call :run_migrations
    call :start_application
    call :show_access_info
) else if "%command%"=="build" (
    call :log_info "仅构建应用..."
    call :build_application
) else if "%command%"=="migrate" (
    call :log_info "仅运行数据库迁移..."
    call :run_migrations
) else if "%command%"=="docker" (
    call :log_info "Docker 模式启动..."
    call :start_infrastructure
    call :start_application
    call :show_access_info
) else if "%command%"=="clean" (
    call :log_info "清理..."
    call :cleanup
) else if "%command%"=="help" (
    echo Matrix Service Discovery Windows 启动脚本
    echo.
    echo 用法: %0 [命令]
    echo.
    echo 命令:
    echo   start    - 完整启动应用 (默认)
    echo   build    - 仅构建应用
    echo   migrate - 仅运行数据库迁移
    echo   docker   - Docker 模式启动
    echo   clean    - 清理资源
    echo   help     - 显示帮助信息
) else (
    call :log_error "未知命令: %command%"
    call :log_info "使用 '%0 help' 查看帮助信息"
    exit /b 1
)

goto :eof

:: 执行主函数
call :main %*